package com.jldt.flutter_douyin;

import android.app.Activity;
import android.util.Log;

import androidx.annotation.NonNull;

import com.bytedance.sdk.open.aweme.authorize.model.Authorization;
import com.bytedance.sdk.open.douyin.DouYinOpenApiFactory;
import com.bytedance.sdk.open.douyin.DouYinOpenConfig;
import com.bytedance.sdk.open.douyin.api.DouYinOpenApi;

import java.util.List;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class FlutterMethodChannel implements MethodChannel.MethodCallHandler {
    public static final String CHANNEL_NAME = "flutter_douyin";
    public static final String INVOKE_NAME = "ANDROID_DouyinOpenSDKAuthRequest";
    public static final String TAG = "flutter_douyin";

    private MethodChannel channel;
    private Activity activity;


    private static volatile FlutterMethodChannel instance;

    private FlutterMethodChannel() {
    }

    public static FlutterMethodChannel getInstance() {
        if (instance == null) {
            synchronized (FlutterMethodChannel.class) {
                if (instance == null) {
                    instance = new FlutterMethodChannel();
                    Log.e("FlutterMethodChannel...", "[完成]");
                }
            }
        }
        return instance;
    }

    public void init(@NonNull Activity activity, @NonNull BinaryMessenger messenger) {
        this.activity = activity;
        channel = new MethodChannel(messenger, CHANNEL_NAME);
        channel.setMethodCallHandler(this);


    }

    public void invokeMethod(@NonNull Object arguments) {
        if (channel != null) {
            channel.invokeMethod(INVOKE_NAME, arguments);
        }
    }


    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        if (call.method.equals("getPlatformVersion")) {
            result.success("Android " + android.os.Build.VERSION.RELEASE);
        } else if (call.method.equals("setPlatformKey")) {
            String androidKey = call.argument("androidKey");
            DouYinOpenApiFactory.init(new DouYinOpenConfig(androidKey));
            result.success(androidKey);
        } else if (call.method.equals("douyinOpenSDKAuthRequest")) {

            List<String> permissionMest = call.argument("permissionMest");
            List<String> permissionOptional = call.argument("permissionOptional");

            DouYinOpenApi douyinOpenApi = DouYinOpenApiFactory.create(activity);
            Authorization.Request request = new Authorization.Request();
            request.scope = permissionMest.toString().replace("[", "").replace("]", "");

            if (permissionOptional != null) {
                if (!permissionOptional.isEmpty()) {
                    request.optionalScope1 = permissionOptional.toString().replace("[", "").replace("]", "");
                    Log.e(TAG,request.optionalScope1);
                }
            }
            request.callerLocalEntry = "com.jldt.flutter_douyin.douyinapi.DouYinEntryActivity";

            Log.e(TAG,request.scope);


            douyinOpenApi.authorize(request);
            result.success("success");
        } else {
            result.notImplemented();
        }
    }
}

